const {getDmg,reduceResistanceK,lvSuppressK,objValueAdd } = require("../libs/apis.js")

const WEAPONS = require("./weapons.js");

const CLIPDATA_TYPE = {
    max:{
        fixedhp: 298.75,
        fixedAtt: 19.45,
        fixdefence: 23.15,
        reaction : 23.31,
        attRate: 5.83, 
        hpRate: 5.83, 
        criticalRate: 3.89,
        criticalStrike: 7.77,
        defRate: 7.29,
        charging: 6.48
    },
    mid:{
        fixedhp: 253.94,   // 209.13  239   268.88   298.75
        fixdefence: 19.675, // 16.2   18.52  20.83  23.15
        fixedAtt: 16.535,   //  13.62   15.56   17.51  19.45
        reaction : 19.815,  // 16.32   18.65  20.98  23.31
        attRate: 4.955,  // 4.08  4.66  5.25  5.83
        hpRate: 4.955,   // 4.08  4.66  5.25  5.83
        criticalRate: 3.305,   //  2.72  3.11   3.5   3.89
        criticalStrike: 6.605,  //  5.44  6.22  6.99  7.77
        defRate: 6.195,   //  5.1  5.83  6.56   7.29
        charging: 5.505  //  4.53   5.18   5.83   6.48
    }
}

const CLIPDATA = CLIPDATA_TYPE.mid

const CONFIG = {
    chance: 4,
    your_lv: 90,
    enemy_lv: 90,
    clips_main: {
        fixedHp: 4780,
        fixedAtt:311,
        attRate: 46.6,
        dmgRate: 46.6,
        criticalStrike: 62.2,
    },
    clips_initial: [
        ["charging", "attRate", "criticalRate", "criticalStrike"],
        ["charging", "attRate", "criticalRate", "criticalStrike"],
        ["charging", "attRate", "criticalRate", "criticalStrike"],
        ["charging", "attRate", "criticalRate", "criticalStrike"],
        ["charging", "attRate", "fixedAtt", "criticalRate"]
    ],
    reactionK : 1.5,
    reactionDmgAdd: 0,
    buffs: [  
        { attRate:18, dmgRate:18*3},  // 生命之契套 
        { attRate:25+20, fixedAtt: 1203 },  // 班尼
        { attRate:20,a1Dmg:16, dmgRate:40 , jk:40 },  // 万叶
        { dmgRate:25} , // 夜
    ]
}

let charaters = {
    units : {
        name: "仆人", 
        dmgZhi: 0,
        charging: 100,
        defRate: 0,reaction:0,
        defence: 765.71,
        weapontype: "qiang",
        attRate: 0,
        hp:13103,
        base: 342.03,    
        criticalRate: 5,    
        criticalStrike: 50 + 38.4,
        skills: {
            a: [
                function(data){},
            ]
        }
    },
}

let tmp = {}
CONFIG.clips_initial.forEach(item => {
    item.forEach(item2 => {
        if(tmp[item2]) {
            tmp[item2] += 1
        } else {
            tmp[item2] = 1
        }
    })
});
/*
    a: 生命之契>=30% a附魔火
        礼赞状态普攻带红死之宴 附加伤害值(消耗一次生命之契 7.5%)
        lv10: 93.9 103 129.25 73.43x2 138.34 168.77      +238    z:256.4
        lv13: 113.78 124.81 156.62 88.98x2 167.63 204.51      +288.4    z:256.4

    q: 666.7%
*/

let cons = 0 // 命座
let chooseIndex = 0; // 查看结果索引
let cid = "units"
let count_kit = 0;
let combination = getCombination(Array(5*CONFIG.chance + 1).fill().map((_,i)=>i), CONFIG.chance, Object.entries(tmp))
let clips_initial_data = calc_clips_initial()
let fs = require("fs")
calc(combination);
function calc(clipSecondData) {
    let charactersDmg = [];
    clipSecondData = clipSecondData.map(clipSecondDataItem => {
        let clipSecond = {}
        clipSecond.des = clipSecondDataItem;
        clipSecond.data = {} 
        for (let secondKey in clipSecond.des) {
            let count =  clipSecond.des[secondKey]
            clipSecond.data[secondKey] = count * CLIPDATA[secondKey]
        }
        return clipSecond
    })
    
    Object.values(WEAPONS).forEach(weapon => {
        if(charaters[cid].weapontype && weapon.weapontype != charaters[cid].weapontype && weapon.weapontype!="all") {return}
        charactersDmg.push(getDMG([charaters[cid],weapon,clips_initial_data,{...CONFIG.clips_main}], clipSecondData, (data,second) => {
            count_kit++;
            data = objValueAdd([data, ...CONFIG.buffs]);
            data.hp = data.hp*(1 + (data.hpRate||0)/100) + data.fixedHp;
            data.dmgRate += 40
            
            if(weapon.skill) { weapon.skill(data); }
            
            let rea = { reaction:data.reaction, reactionQ:CONFIG.reactionK, reactionDmgAdd:data.reactionDmgAdd || 0 }
            // 0.6359*2 1.2199 1.5859 0.8282*2 1.8887 1.6174
            let aRateList = [  // 22.0015 12.33
                0.939 + 2.38,   // 1 
                1.03 + 2.38,   // 2
                1.2925 + 2.38,   // 3
                0.7343 + 2.38,   // 4
                0.7343,   // 4
                1.3834 + 2.38,   // 5
                1.6877 + 2.38  // 6
            ]
            
            let a1puDmg = getDmg({
                data: {...data,dmgRate: data.dmgRate + (data.a1Dmg||0),}, rate: aRateList[0]
            }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);
            let a2puDmg = getDmg({
                data: {...data,dmgRate: data.dmgRate + (data.a1Dmg||0),}, rate: aRateList[1]
            }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);
            let a3puDmg = getDmg({
                data: {...data,dmgRate: data.dmgRate + (data.a1Dmg||0),}, rate: aRateList[2]
            }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);
            let a4puDmg = getDmg({
                data: {...data,dmgRate: data.dmgRate + (data.a1Dmg||0),}, rate: aRateList[3]
            }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);
            let a5puDmg = getDmg({
                data: {...data,dmgRate: data.dmgRate + (data.a1Dmg||0),}, rate: aRateList[4]
            }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);
            let a6puDmg = getDmg({
                data: {...data,dmgRate: data.dmgRate + (data.a1Dmg||0),}, rate: aRateList[5]
            }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);
            let a7puDmg = getDmg({
                data: {...data,dmgRate: data.dmgRate + (data.a1Dmg||0),}, rate: aRateList[6]
            }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);
            
            let a1puDmgRea = getDmg({
                data: {...data,dmgRate: data.dmgRate + (data.a1Dmg||0),},rea, rate: aRateList[0]
            }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);
            let a2puDmgRea = getDmg({
                data: {...data,dmgRate: data.dmgRate + (data.a1Dmg||0),},rea, rate: aRateList[1]
            }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);
            let a3puDmgRea = getDmg({
                data: {...data,dmgRate: data.dmgRate + (data.a1Dmg||0),},rea, rate: aRateList[2]
            }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);
            let a4puDmgRea = getDmg({
                data: {...data,dmgRate: data.dmgRate + (data.a1Dmg||0),},rea, rate: aRateList[3]
            }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);
            let a5puDmgRea = getDmg({
                data: {...data,dmgRate: data.dmgRate + (data.a1Dmg||0),},rea, rate: aRateList[4]
            }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);
            let a6puDmgRea = getDmg({
                data: {...data,dmgRate: data.dmgRate + (data.a1Dmg||0),},rea, rate: aRateList[5]
            }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);
            let a7puDmgRea = getDmg({
                data: {...data,dmgRate: data.dmgRate + (data.a1Dmg||0),},rea, rate: aRateList[6]
            }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);
            

            let qRate = 6.6672
            
            let qDmg = getDmg({
                data: {
                    ...data,
                    dmgRate: data.dmgRate + (data.qDmgRate||0),
                },
                rate: qRate[0]
            }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);

            let qreaDmg = getDmg({
                data: {
                    ...data,
                    dmgRate: data.dmgRate + (data.qDmgRate||0),
                },
                rate: qRate[0], rea
            }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);

            let point = (a1puDmg + a2puDmg + a3puDmg + a4puDmg + a5puDmg + a6puDmg + a7puDmg) * 3 + 
            (a1puDmgRea + a2puDmgRea + a3puDmgRea + a4puDmgRea + a5puDmgRea + a6puDmgRea + a7puDmgRea) * 3 +
            qDmg;
            
            
            let attack = data.base *(1 + data.attRate/100) + data.fixedAtt;
            return {point, data, second, attack, dmg:{
                qDmg, qreaDmg, aall: a1puDmg + a2puDmg + a3puDmg + a4puDmg + a5puDmg + a6puDmg + a7puDmg ,
                aReaAll: a1puDmgRea + a2puDmgRea + a3puDmgRea + a4puDmgRea + a5puDmgRea + a6puDmgRea + a7puDmgRea,
                a1puDmg, a2puDmg, a3puDmg, a4puDmg, a5puDmg, a6puDmg, a7puDmg
            }}
        }))
    })
    
    charactersDmg.sort((a,b)=>{
        return (b.point)-(a.point);
    });
    let sortf = JSON.parse(fs.readFileSync("./sort.js").toString()) 
    let choose = charactersDmg[chooseIndex];
    console.log(choose);
    // fs.writeFileSync("./ret.js", JSON.stringify(choose))

    let name = choose.data.name
    let keys = "_"+choose.data.weaponname+"_6命无拐2+2毕业"
    let sortfData = {key: name+keys, point:choose.point, dmg:JSON.parse(JSON.stringify(choose.dmg))}
    if(sortfData.point && sortfData.point > 0) {
        let org = sortf.findIndex(item3 => item3.key == sortfData.key)
        if(org >= 0){
            sortf[org] = sortfData
        } else {
            sortf.push(sortfData)
        }
        sortf.sort((b,a) => a.point-b.point)
        // fs.writeFileSync("./sort.js", JSON.stringify(sortf))
    }
    console.log(charactersDmg.map((v, n)=>{
        return  `(${n})得分: ${v.data.weaponname} : ${v.point.toFixed(0)}`
    }));
    console.log(count_kit);
}

function getDMG(dataarr, clipSecond, getRet){
    clipSecond = clipSecond.map(second => {
        let panel = objValueAdd([...dataarr, second.data]);
        return  getRet(panel, second)
    })
    clipSecond.sort((b,a)=> a.point-b.point)
    return clipSecond[0]
}

function calc_clips_initial () {
    let clips = CONFIG.clips_initial.map(v => {
        let tmp = {};
        v.forEach(v2 => {
            tmp[v2] = CLIPDATA[v2]
        })
        return tmp
    })
    let data = {}
    clips.forEach(item => {
        for (const key in item) {
            data[key] = data[key]? (data[key]+item[key]) : item[key]
        }
    })
    return data
}
function getCombination (arr, chance, entrys) {
    let num = 5
    var r = [];
    (function f(t, a, n) {
        if (n === 0) {
            if(t.reduce((a1,b1) => a1+b1) !== chance * 5){return}
            let ret = {}
            for (let i in t) {
                if(t[i] > entrys[i][1]*chance){return}
                ret[entrys[i][0]] = t[i]
            }
            r.push(ret)
            return ;
        }
        for (var i = 0, l = a.length; i < l; i++) {
            f(t.concat(a[i]), a, n - 1);
        }
    })([], arr, num);
    return r;
}